package com.yanqu.road.dao.impl.taobao;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import taobao.config.TaoBaoItemConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TaoBaoItemConfigDaoImpl extends TempDao {

    public static String sql = "select * from t_s_activity_taobao_item where activityId = ?";

    public Map<Integer, TaoBaoItemConfig> getConfigMap(int activityId) {
        Map<Integer, TaoBaoItemConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                TaoBaoItemConfig config = new TaoBaoItemConfig();
                config.setActivityId(rs.getInt("activityId"));
                config.setItemId(rs.getInt("itemId"));
                config.setType(rs.getInt("type"));
                config.setIcon(rs.getString("icon"));
                config.setSellNpc(rs.getInt("sellNpc"));
                config.setPositions(rs.getString("positions"));
                config.setPosNpc(rs.getString("posNpc"));
                map.put(config.getItemId(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
